<?php

namespace app\web\model;
use think\Db;
use think\Model;
use think\Session;

class MycenterModel extends Model
{
    public function showAll()
    {
       if(empty(session::get('userid'))){
           $userid = $_COOKIE['userid'];
       }else{
           $userid = session::get('userid');
       }
        // $userid = 6;
//        $data = Db::table(['user', 'order'])
//                    ->field('user.name, order.id, order.count, order.price')
//                    ->where([
//                        'user.id'=>$userid,
//                        'order.userid'=>$userid
//                    ])
//                    ->select();

        // var_dump($userid);die;
        $data = Db::table(['user', 'order'])
            ->field('user.nickname, order.id, order.count, order.price, order.status')
            ->where([
                'user.id'=>$userid,
                'order.userid'=>$userid
            ])
            ->paginate(2);
//            ->each(function($item,$key){
//               $item['nickname'] = 'think';
//                return $item;
//            });

//        var_dump($data);die;
       // print_r($data);

        return $data;

    }

    public function showOk()
    {
        if(empty(session::get('userid'))){
           $userid = $_COOKIE['userid'];
       }else{
           $userid = session::get('userid');
       }
        // $userid = 6;

        $data = Db::table(['user', 'order'])
            ->field('user.nickname, order.id, order.count, order.price')
            ->where([
                'user.id'=>$userid,
                'order.userid'=>$userid,
                'order.status'=>2
            ])
            ->paginate(2);

//        var_dump($data);die;
//        print_r($data);

        return $data;
    }

    public function showNot()
    {
        if(empty(session::get('userid'))){
           $userid = $_COOKIE['userid'];
       }else{
           $userid = session::get('userid');
       }
        // $userid = 6;

        $data = Db::table(['user', 'order'])
            ->field('user.nickname, order.id, order.count, order.price')
            ->where([
                'user.id'=>$userid,
                'order.userid'=>$userid,
                'order.status'=>1
            ])
            ->paginate(2);

//        var_dump($data);die;
//        print_r($data);

        return $data;
    }

    public function showGoods($id)
    {
        if(empty(session::get('userid'))){
           $userid = $_COOKIE['userid'];
       }else{
           $userid = session::get('userid');
       }
        // $userid = 6;
        $oid = $id;
//        var_dump($oid);die;
        $data = Db::table(['order_goods'])
                        ->field('order_goods.count, order_goods.price, order_goods.gid')
                        ->where([
                            'order_goods.oid'=>$oid
                        ])
                        ->select();
//        var_dump($data);die;

        $reu = Db::table('order') ->field('status') ->where('id',$oid) ->select();
//        var_dump($reu);die;

        for($d=0; $d<count($data); $d++){
            $res = Db::table('goods')
                            ->field('pictrue, name, price')
                            ->where('id',$data[$d]['gid'])
                            ->select();
//            var_dump($res);die;

            $data[0]['status'] = $reu[0]['status'];
            $data[$d]['pictrue'] = $res[0]['pictrue'];
            $data[$d]['name'] = $res[0]['name'];
            $data[$d]['Oprice'] = $res[0]['price'];

//            var_dump($data);
        }
//        var_dump($data);die;
//        echo 111;die;
        return $data;
    }

    public function gopay($id)
    {
        if(empty(session::get('userid'))){
           $userid = $_COOKIE['userid'];
       }else{
           $userid = session::get('userid');
       }
        // $userid = 6;

        $status = ['status'=> 2];
        $ok = Db::table('order') ->where('id', $id) ->update($status);

        $data = Db::table('order')
                    ->field('price')
                    ->where('userid', $userid)
                    ->select();
//        var_dump($data);die;

        $payorder = 0;

        for($p=0; $p<count($data); $p++){
            $payorder += $data[$p]['price'];
        }
//        var_dump($payorder);die;

        if($payorder >= 50){

            $vmess = Db::table('vip') ->field('id') ->where('userid', $userid) ->find();

            $levelmessa = Db::table('level') ->order('payorder') ->select();
//            var_dump($levelmessa);die;

//            if($payorder >= 1000){
//                $vip['level'] = 5;
//            }elseif($payorder >= 800){
//                $vip['level'] = 4;
//            }elseif($payorder >= 600){
//                $vip['level'] = 3;
//            }elseif($payorder >= 400){
//                $vip['level'] = 2;
//            }elseif($payorder >= 200){
//                $vip['level'] = 1;
//            }

//            var_dump($levelmessa);die;

            for($i=0; $i<count($levelmessa); $i++){
                if($payorder >= $levelmessa[$i]['payorder']){
                    $vip['level'] = $levelmessa[$i]['level'];
                }
            }

//            var_dump($vip['level']);die;
            $vip['payorder'] = $payorder;

            $mm = Db::table('user') -> field('nickname, tel') ->where('id', $userid) ->find();

            $vip['userid'] = $userid;
            $vip['name'] = $mm['nickname'];
            $vip['tel'] = $mm['tel'];
            $vip['time'] = 12345;

            if(!empty($vmess)){
                $update = Db::table('vip') ->where('userid', $userid) ->update($vip);
            }else{
                $insert = Db::table('vip') ->insert($vip);
            }
        }
//        var_dump($ok);die;
        return $ok;
    }
}
















